package com.vay.de.dang.online.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.vay.de.dang.online.util.VnUtil;
import com.vay.de.dang.online.R;

public class CameraClipView extends SurfaceView {

    final static float f0 = 0.62f;
    final static float f = 1.574f;
    private SurfaceHolder mHolder;
    private Bitmap iconTl;
    private Bitmap iconTr;
    private Bitmap iconBl;
    private Bitmap iconBr;
    private Thread thread;
    private boolean running;
    private Rect clipRect = new Rect();
    private Rect outRect = new Rect();
    private Rect r1 = new Rect();
    private Rect r2 = new Rect();
    private Rect r3 = new Rect();
    private Rect r4 = new Rect();
    private Paint paint = new Paint();

    public CameraClipView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mHolder = getHolder();
        mHolder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
                checkThread();
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
                running = false;
                thread = null;
                VnUtil.recyBitmap(iconTl);
                VnUtil.recyBitmap(iconTr);
                VnUtil.recyBitmap(iconBl);
                VnUtil.recyBitmap(iconBr);
                iconTl = iconTr = iconBl = iconBr = null;
                setKeepScreenOn(false);
            }
        });

        setZOrderOnTop(true);
        mHolder.setFormat(PixelFormat.TRANSLUCENT);
        paint.setColor(Color.parseColor("#8A000000"));
        setKeepScreenOn(true);
    }

    private void checkThread() {
        running = true;
        thread = new Thread(() -> {
            while (running) {
                Canvas canvas = null;
                try {

                    canvas = mHolder.lockCanvas();
                    if (canvas == null) {
                        Thread.sleep(5);
                        continue;
                    }

                    canvas.save();
                    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                    drawClip(canvas);
                    canvas.restore();

                } catch (Throwable e) {
                    e.printStackTrace();
                } finally {
                    if (canvas != null && mHolder != null) {
                        mHolder.unlockCanvasAndPost(canvas);
                    }
                }

                try {
                    Thread.sleep(33);
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

    private void drawClip(Canvas canvas) {

        final int width = getWidth();
        final int height = getHeight();
        if (width <= 0 || height <= 0) {
            return;
        }

        int cw = (int) (width * f0);
        int ch = (int) (cw / f);
        int left = (width - cw) / 2;
        int top = (height - ch) / 2;

        if (iconTl == null) {
            iconTl = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_clip_rect_t_l);
            iconTr = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_clip_rect_t_r);
            iconBl = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_clip_rect_b_l);
            iconBr = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_clip_rect_b_r);

            r1.set(0, 0, width, (height - ch) / 2);
            r2.set(0, top, (width - cw) / 2, top + ch);
            r3.set(left + cw, top, width, top + ch);
            r4.set(0, height - (height - ch) / 2 - 1, width, height);

            clipRect.set(left, top, left + cw, top + ch);
        }

        /*canvas.save();
        canvas.clipOutRect(clipRect);
        canvas.drawColor(Color.parseColor("#8a000000"));
        canvas.restore();*/

        canvas.drawRect(r1, paint);
        canvas.drawRect(r2, paint);
        canvas.drawRect(r3, paint);
        canvas.drawRect(r4, paint);

        // 边框
        canvas.drawBitmap(iconTl, left, top, null);
        canvas.drawBitmap(iconBl, left, top + ch - iconBl.getHeight(), null);
        canvas.drawBitmap(iconTr, width - left - iconTr.getWidth(), top, null);
        canvas.drawBitmap(iconBr, width - left - iconBr.getWidth(), top + ch - iconBr.getHeight(), null);
    }

    public Rect getClipRect(Bitmap src) {

        // 裁剪矩形的大小
        int w = (int) (src.getWidth() * f0);
        int h = (int) (w / f);

        int left = (src.getWidth() - w) / 2;
        int top = (src.getHeight() - h) / 2;

        outRect.set(left, top, left + w, top + h);
        
        return outRect;
    }

}
